﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tamir.IPLib.Packets;
using Tamir.IPLib;
using System.Net.Sockets;
using System.Diagnostics;
using System.Collections;
using System.Threading;

namespace HostDetector
{
    class PacketSniffer
    {
        //PcapDevice device;
        ArrayList openDevices;
        ThreadStart sniffJob;
        Thread sniffThread;
        PcapDevice deviceToStart;
        public delegate void  packetDel(UDPPacket packet);
        public event packetDel packetEvent;
        public static List<string> localAddress = new List<string>();
        
        public PacketSniffer()
        {
            
        }
        
        public void connect()
        {
            openDevices = new ArrayList();
            /* Retrieve the device list */
            PcapDeviceList devices = SharpPcap.GetAllDevices();

            /*If no device exists, print error */
            if (devices.Count < 1)
            {
                Console.WriteLine("No device found on this machine");
                return;
            }

            int i = 0;

            /* Scan the list printing every entry */
            foreach (PcapDevice device in devices)
            {
            //PcapDevice device = devices[0];
 
                /* Description */
                Console.WriteLine("{0}) {1}", i, device.PcapDescription);
                Console.WriteLine();
                /* Name */
                Console.WriteLine("\tName:\t{0}", device.PcapName);
                /* IP Address */
                Console.WriteLine("\tIP Address: \t\t{0}", device.PcapIpAddress);
                /* Is Loopback */
                Console.WriteLine("\tLoopback: \t\t{0}", device.PcapLoopback);

                Console.WriteLine();
                i++;


                //Register our handler function to the 

            //'packet arrival' event

            device.PcapOnPacketArrival +=
              new SharpPcap.PacketArrivalEvent(device_PcapOnPacketArrival);

            //Open the device for capturing

            //true -- means promiscuous mode

            //1000 -- means a read wait of 1000ms

            device.PcapOpen(false, 1000);

            //tcpdump filter to capture only TCP/IP packets            

            string filter = "ip and udp";
            //Associate the filter with this capture

            device.PcapSetFilter(filter);
            //device.PcapIpAddress    
            Console.WriteLine();
            Console.WriteLine
              ("-- The following tcpdump filter will be applied: \"{0}\"",
              filter);
            Console.WriteLine
              ("-- Listenning on {0}, hit 'Enter' to stop...",
              device.PcapDescription);
            //pp.LocalIP = device.PcapIpAddress;
            //Start capture packets
            deviceToStart = device;
            sniffJob = new ThreadStart(startDevice);
            sniffThread = new Thread(sniffJob);
            sniffThread.Start();
            //localAddress.Add(((Tamir.IPLib.NetworkDevice)(device)).IpAddress);
            
            }
           
            
            
        }

        private void startDevice()
        {
            openDevices.Add(deviceToStart);
            deviceToStart.PcapStartCapture();
            //device.PcapCapture(1);//SharpPcap.INFINITE);

            
        }
        public void disconnect()
        {
            foreach (PcapDevice device in openDevices)
            {
                Debug.WriteLine("Closing: " + device.PcapDescription);
                device.PcapStopCapture();

                device.PcapClose();
            }
        }

        

        private void device_PcapOnPacketArrival(
                         object sender, Packet packet)
        {
            if (packet is UDPPacket)
            {
                if (packetEvent != null)
                    packetEvent(packet as UDPPacket);

            }
        }


    }
}